//
//  LYMainViewController.swift
//  LYOnlineBanking
//
//  Created by 李永彬 on 2025/7/15.
//

import UIKit
import SnapKit

class LYMainViewController: UIViewController, LYCustomTabBarViewDelegate {

    var customTabBar = LYCustomTabBarView()
    private var currentViewController: UIViewController?
    
    private let viewControllers: [UIViewController] = [
        LYNavigationController(rootViewController: LYEmohViewController()),
        LYNavigationController(rootViewController: LYOrderViewController()),
        LYNavigationController(rootViewController: LYUserViewController())
    ]
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupCustomTabBar()
        setupViewControllers()
    }
    
    private func setupCustomTabBar() {
        customTabBar.backgroundColor = UIColor(hex: "#000000")
        customTabBar.layer.masksToBounds = true
        customTabBar.layer.cornerRadius = customTabBarHeight / 2.0
        customTabBar.delegate = self
        customTabBar.itemIcons = [
            UIImage(named: "home_tab_normal")!,
            UIImage(named: "order_tab_normal")!,
            UIImage(named: "personal_tab_normal")!,
        ]
        customTabBar.selectedItemIcons = [
            UIImage(named: "home_tab_selected")!,
            UIImage(named: "order_tab_selected")!,
            UIImage(named: "personal_tab_selected")!,
        ]
        customTabBar.selectedColor = .systemBlue
        
        view.addSubview(customTabBar)
        customTabBar.snp.makeConstraints { make in
            make.bottom.equalToSuperview().offset(-bottomMargin)
            make.height.equalTo(customTabBarHeight)
            make.leading.trailing.equalToSuperview().inset(14)
        }
        customTabBar.barHeight = customTabBarHeight
    }
    
    func setCustomTabBarHidden(_ hidden: Bool) {
        customTabBar.isHidden = hidden
    }
    
    private func setupViewControllers() {
        // 初始显示第一个视图控制器
        changeViewController(to: 0)
    }
    
    func changeViewController(to index: Int) {
        // 移除当前显示的视图控制器
        currentViewController?.willMove(toParent: nil)
        currentViewController?.view.removeFromSuperview()
        currentViewController?.removeFromParent()
        
        // 添加新的视图控制器
        let newVC = viewControllers[index]
        if let nav = newVC as? LYNavigationController,
           let vc = nav.viewControllers.first as? LYEmohViewController  {
            vc.updateData()
        } else if let nav = newVC as? LYNavigationController,
                  let vc =  nav.viewControllers.first as? LYOrderViewController {
            vc.updateData(tab: "")
        } else if let nav = newVC as? LYNavigationController,
                  let vc =  nav.viewControllers.first as? LYUserViewController {
            vc.updateData()
        }
        addChild(newVC)
        view.insertSubview(newVC.view, belowSubview: customTabBar)
        newVC.view.frame = view.bounds
        newVC.didMove(toParent: self)
        customTabBar.selectedIndex = index
        
        currentViewController = newVC
    }
    
    // MARK: - CustomTabBarDelegate
    
    func tabBar(_ tabBar: LYCustomTabBarView, didSelectItemAt index: Int) {
        changeViewController(to: index)
    }
}
